from django.db import models
from user.models import User
# Create your models here.


# 训练动作表
class Exercise(models.Model):
    """
    健身动作的基本信息   id 动作的唯一标识

    {
    "name": "标准俯卧撑",
    "description": "锻炼胸肌、三角肌和肱三头肌的基础动作",
    "difficulty": "intermediate",
    "target_muscles": "胸部,肩部,手臂",
    "estimated_duration": 120
    }
    """

    DIFFICULTY_CHOICES = [
        ('beginner', '初级'),
        ('intermediate', '中级'),
        ('advanced', '高级'),
    ]
    name = models.CharField(max_length=100, verbose_name="动作名称")   # 动作名称（如：深蹲、俯卧撑
    description = models.TextField(blank=True, verbose_name="动作描述")   # 动作的详细描述和要领
    difficulty = models.CharField(max_length=20, choices=DIFFICULTY_CHOICES, default='intermediate',
                                  verbose_name="难度")
    target_muscles = models.CharField(max_length=200, verbose_name="目标肌群")  # 目标锻炼的肌群（如：胸部、腿部）
    demonstration_video = models.URLField(blank=True, verbose_name="演示视频")   # 动作演示视频的URL
    estimated_duration = models.PositiveIntegerField(default=60, verbose_name="预估时长(秒)")   # 预估完成该动作的时长（秒）

    class Meta:
        db_table = 'tb_exercise'
        verbose_name = '训练动作表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name



# 健身计划表
class WorkoutPlan(models.Model):
    """
    完整的健身计划框架  id 计划的唯一标识

    {
    "title": "新手全身训练计划",
    "description": "适合初学者的全身综合性训练",
    "difficulty": "beginner",
    "total_duration": 1800,
    "rest_time": 45,
    "created_by": 1  # 用户ID
    }

    """
    DIFFICULTY_CHOICES = [
        ('beginner', '初级'),
        ('intermediate', '中级'),
        ('advanced', '高级'),
    ]
    title = models.CharField(max_length=200, verbose_name="计划标题")  # 计划标题（如：7天减脂计划）
    description = models.TextField(blank=True, verbose_name="计划描述")
    difficulty = models.CharField(max_length=20, choices=DIFFICULTY_CHOICES, default='intermediate',
                                  verbose_name="计划难度")
    total_duration = models.PositiveIntegerField(default=0, verbose_name="计划总时长(秒)")
    rest_time = models.PositiveIntegerField(default=30, verbose_name="计划休息时间(秒)")
    exercises = models.ManyToManyField(Exercise, through='WorkoutPlanExercise', verbose_name="计划包含动作")
    created_by = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="计划创建者")  # 计划创建者（用户ID）
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="计划创建时间")

    class Meta:
        db_table = 'tb_workoutplan'
        verbose_name = '健身计划表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


# 训练计划动作关联表
class WorkoutPlanExercise(models.Model):
    """
    WorkoutPlanExercise（计划-动作关联表）  id 关联记录的唯一标识

    连接计划和动作的中间表，包含具体的训练参数

    {
    "workout_plan": 1,  # 计划ID
    "exercise": 5,      # 动作ID（深蹲）
    "sets": 3,
    "reps": 12,
    "order": 0          # 第一个动作
    }
    """
    workout_plan = models.ForeignKey(WorkoutPlan, on_delete=models.CASCADE)   # 所属健身计划ID
    exercise = models.ForeignKey(Exercise, on_delete=models.CASCADE)   # 包含的动作ID
    sets = models.PositiveIntegerField(default=3, verbose_name="组数")  # 该动作的组数
    reps = models.PositiveIntegerField(default=10, verbose_name="每组次数")  # 每组的重复次数
    order = models.PositiveIntegerField(default=0, verbose_name="顺序")  # 动作在计划中的顺序

    class Meta:
        db_table = 'tb_workoutplan_exercise'
        verbose_name = '训练计划动作关联表'
        verbose_name_plural = verbose_name
        ordering = ['order']



# 用户运动历史表
class UserWorkoutHistory(models.Model):
    """
    记录用户每次完成动作的历史数据  id 历史记录的唯一标识

    {
    "user": 1,          # 用户ID
    "exercise": 5,      # 动作ID（深蹲）
    "duration": 150,    # 锻炼了150秒
    "performance_score": 0.85  # 表现评分85%
    }

    """
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="用户")
    exercise = models.ForeignKey(Exercise, on_delete=models.CASCADE, verbose_name="动作")  # 完成的动作ID
    completed_at = models.DateTimeField(auto_now_add=True, verbose_name="完成时间")
    duration = models.PositiveIntegerField(verbose_name="锻炼时长(秒)")  # 实际锻炼时长（秒）
    performance_score = models.FloatField(default=0.0, verbose_name="表现评分")   # 表现评分（0-1）

    class Meta:
        db_table = 'tb_workout_history'
        verbose_name = '用户运动历史表'
        verbose_name_plural = verbose_name
        ordering = ['-completed_at']